home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-18 | 104.3 KB | 3,331 lines |
- *******************************************************************************
- * PROGRAM: Sql.wfm
- *
- * WRITTEN BY: Borland Samples Group
- *
- * DATE: 3/95
- *
- * UPDATED: 7/95
- *
- * REVISION: $Revision: 1.37 $
- *
- * VERSION: Visual dBASE
- *
- * DESCRIPTION: This is a tool for building a SQL statement. You can also
- * use this tool to see examples of dBASE vs SQL code, and
- * use pre-defined templates for SQL statements.
- * This tool supports local dBASE and paradox tables, as well as
- * Interbase, Oracle and Sybase server tables.
- *
- * PARAMETERS: None
- *
- * CALLS: Buttons.cc (Custom controls file)
- * Getfield.wfm (Field picker)
- * Gettable.wfm (Table picker)
- * Options.pop (Options popup)
- *
- * USAGE: DO Sql.wfm
- *
- ********************************************************************************
- #include <Messdlg.h>
-
- #define ALLTRIM(s) ltrim(rtrim(s))
- #define TRIMSTR(s) ltrim(str(s))
-
- #define MAX_STATEMENTS 4
- #define MAX_AGGREGATES 5
- #define MAX_OPERATORS 8
- #define MAX_ARTICLES 3
- #define MAX_VIEWS 3
- #define MAX_ALLOWED_CLAUSES 5
- #define MAX_INSERT_VALUES 25
- #define PERIOD "."
-
- #define ALL_FIELDS "*"
-
- *** Activity indicators
- #define NO_WARNING 0
- #define DISPLAY_WARNING 1
-
- *** For SQL/dBASE code examples (page 3 of form)
- #define NO_QUERY 0
- #define SQL_QUERY 1
- #define DBASE_QUERY 2
-
- *** Field restrictions
- #define MEMO_BINARY_OLE "M, B, O"
- #define CHAR_LOGICAL_DATE "C, L, D"
- #define NUMERIC "N"
-
- *** INSERT value control constants
- #define INSERT_VALUE_WIDTH 12
- #define INSERT_VALUE_HEIGHT 1.2
- #define INSERT_VALUES_PER_ROW 5
- #define INSERT_START_TOP 4.5
- #define INSERT_START_LEFT 3
-
- *** Constants for referencing table information from IDAPI API
- #define DATABASE_INFO_LEN 224
- #define DATABASE_TYPE_LOC 147
-
- *** Error Messages
- #define ERROR_NO_SUCH_DATABASE 239
-
-
- *** New Session
- create session
-
- *** Environment
- set talk off
- set ldcheck off
- set date mdy && SQL wants american date format
- set mark to
- set century off
- set memowidth to 120
-
-
- ** END HEADER -- do not remove this line*
- * Generated on 07/11/95
- *
- parameter bModal
- local f
- f = new SQLFORM()
- if (bModal)
- f.mdi = .F. && ensure not MDI
- f.ReadModal()
- else
- f.Open()
- endif
- CLASS SQLFORM OF FORM
- Set Procedure To &_dbwinhome.custom\BUTTONS.CC additive
- this.OnOpen = CLASS::FORM_ONOPEN
- this.Left = 1
- this.StatusMessage = "Select a SQL command"
- this.Top = 1
- this.OnClose = CLASS::FORM_ONCLOSE
- this.Width = 77.333
- this.ColorNormal = "N/BTNFACE"
- this.View = "Sql.qbe"
- this.Text = "SQL Statement Builder"
- this.OnMouseMove = CLASS::FORM_ONMOUSEMOVE
- this.Height = 19
-
- DEFINE RECTANGLE STATEMENTRECT OF THIS;
- PROPERTY;
- Left 0.5,;
- Top 0.1973,;
- Width 76.5,;
- Text "",;
- OnMouseMove CLASS::STATEMENTRECT_ONMOUSEMOVE,;
- Height 13.5674
-
- DEFINE COMBOBOX STATEMENTCOMBO OF THIS;
- PROPERTY;
- Left 2.5,;
- Top 0.5,;
- Style 2,;
- Width 13,;
- OnChange CLASS::STATEMENTCOMBO_ONCHANGE,;
- OnMouseMove CLASS::STATEMENTCOMBO_ONMOUSEMOVE,;
- FontBold .F.,;
- Height 1.2646
-
- DEFINE TEXT PAREN1TEXT OF THIS;
- PROPERTY;
- Left 22,;
- Top 1.7979,;
- Width 1,;
- ColorNormal "N/BtnFace",;
- Visible .F.,;
- Text "(",;
- FontBold .F.,;
- Height 0.8262
-
- DEFINE ENTRYFIELD FIELDSENTRY OF THIS;
- PROPERTY;
- Left 23,;
- Enabled .F.,;
- Top 0.5,;
- Width 48,;
- ColorNormal "N/W",;
- Value "",;
- OnMouseMove CLASS::FIELDSENTRY_ONMOUSEMOVE,;
- FontBold .F.,;
- Height 1
-
- DEFINE TEXT PAREN2TEXT OF THIS;
- PROPERTY;
- Left 71,;
- Top 1.7979,;
- Width 1,;
- ColorNormal "N/BtnFace",;
- Visible .F.,;
- Text ")",;
- FontBold .F.,;
- Height 0.8262
-
- DEFINE TOOLBUTTON FIELDSTOOLBUTTON OF THIS;
- PROPERTY;
- SpeedTip "Select fields",;
- Left 72,;
- Top 0.5,;
- Width 4,;
- Group .T.,;
- OnClick CLASS::FIELDSTOOLBUTTON_ONCLICK,;
- OnMouseMove CLASS::FIELDSTOOLBUTTON_ONMOUSEMOVE,;
- FontBold .F.,;
- Height 1
-
- DEFINE TEXT STATEMENTTEXT OF THIS;
- PROPERTY;
- Left 2.5,;
- Top 1.7998,;
- Width 8,;
- ColorNormal "N/BTNFACE",;
- Text "FROM",;
- FontBold .F.,;
- Height 1.0137
-
- DEFINE TEXT INSERTINTOTEXT OF THIS;
- PROPERTY;
- Left 17,;
- Top 0.5,;
- Width 5,;
- ColorNormal "N/BtnFace",;
- Visible .F.,;
- Text "INTO",;
- FontBold .F.,;
- Height 0.8525
-
- DEFINE ENTRYFIELD TABLESENTRY OF THIS;
- PROPERTY;
- Left 23,;
- Enabled .F.,;
- Top 1.7979,;
- Width 48,;
- ColorNormal "N/W",;
- Value "",;
- OnMouseMove CLASS::TABLESENTRY_ONMOUSEMOVE,;
- FontBold .F.,;
- Height 1.0137
-
- DEFINE TOOLBUTTON TABLESTOOLBUTTON OF THIS;
- PROPERTY;
- SpeedTip "Select tables",;
- Left 72,;
- Top 1.7979,;
- Width 4,;
- Visible .F.,;
- Group .T.,;
- OnClick CLASS::TABLESTOOLBUTTON_ONCLICK,;
- OnMouseMove CLASS::TABLESTOOLBUTTON_ONMOUSEMOVE,;
- FontBold .F.,;
- Height 1.0137
-
- DEFINE TOOLBUTTON ADDCLAUSEBUTTON OF THIS;
- PROPERTY;
- Left 2.5,;
- Top 3.1172,;
- Width 13,;
- Text "Where",;
- Group .T.,;
- OnClick CLASS::ADDCLAUSEBUTTON_ONCLICK,;
- OnMouseMove CLASS::ADDCLAUSEBUTTON_ONMOUSEMOVE,;
- FontBold .F.,;
- Height 1.0068
-
- DEFINE TOOLBUTTON GROUPBYBUTTON OF THIS;
- PROPERTY;
- Left 2.5,;
- Top 10.5,;
- Width 13,;
- Visible .F.,;
- Text "Group By",;
- Group .T.,;
- OnClick CLASS::GROUPBYBUTTON_ONCLICK,;
- OnMouseMove CLASS::GROUPBYBUTTON_ONMOUSEMOVE,;
- FontBold .F.,;
- Height 1
-
- DEFINE TOOLBUTTON ORDERBYBUTTON OF THIS;
- PROPERTY;
- Left 2.5,;
- Top 12.5879,;
- Width 13,;
- Visible .F.,;
- Text "Order By",;
- Group .T.,;
- OnClick CLASS::ORDERBYBUTTON_ONCLICK,;
- OnMouseMove CLASS::ORDERBYBUTTON_ONMOUSEMOVE,;
- FontBold .F.,;
- Height 0.9736
-
- DEFINE PUSHBUTTON RUNSQLBUTTON OF THIS;
- PROPERTY;
- Left 0.5,;
- Top 16.3975,;
- Width 14.166,;
- PageNo 0,;
- Text "&Run",;
- Group .T.,;
- OnClick CLASS::RUNSQLBUTTON_ONCLICK,;
- OnMouseMove CLASS::RUNSQLBUTTON_ONMOUSEMOVE,;
- FontBold .F.,;
- UpBitmap "RESOURCE #108",;
- Height 1.5391
-
- DEFINE PUSHBUTTON CLEARSQLBUTTON OF THIS;
- PROPERTY;
- Left 16,;
- Top 16.3975,;
- Width 14.166,;
- PageNo 0,;
- Text "&Clear",;
- Group .T.,;
- OnClick CLASS::CLEARSQLBUTTON_ONCLICK,;
- OnMouseMove CLASS::CLEARSQLBUTTON_ONMOUSEMOVE,;
- FontBold .F.,;
- UpBitmap "RESOURCE #620",;
- Height 1.5391
-
- DEFINE PUSHBUTTON SHOWSQLBUTTON OF THIS;
- PROPERTY;
- Left 31.5,;
- Top 16.3975,;
- Width 14.166,;
- PageNo 0,;
- Text "&Show SQL",;
- Group .T.,;
- OnClick CLASS::SHOWSQLBUTTON_ONCLICK,;
- OnMouseMove CLASS::SHOWSQLBUTTON_ONMOUSEMOVE,;
- FontBold .F.,;
- UpBitmap "RESOURCE #621",;
- Height 1.5391
-
- DEFINE OKBUTTON OKBUTTON1 OF THIS;
- PROPERTY;
- Left 47,;
- Top 16.3975,;
- Width 14.166,;
- PageNo 0,;
- Group .T.,;
- OnClick CLASS::PAGE1OKBUTTON1_ONCLICK,;
- OnMouseMove CLASS::OKBUTTON1_ONMOUSEMOVE,;
- FontBold .F.,;
- Height 1.5391
-
- DEFINE CANCELBUTTON CANCELBUTTON1 OF THIS;
- PROPERTY;
- Left 62.5977,;
- Top 16.3975,;
- Width 14.2354,;
- PageNo 0,;
- Group .T.,;
- OnMouseMove CLASS::CANCELBUTTON1_ONMOUSEMOVE,;
- FontBold .F.,;
- Height 1.5391
-
- DEFINE RECTANGLE MESSAGERECT OF THIS;
- PROPERTY;
- Left 0.5,;
- Top 13.7637,;
- Width 76.5,;
- Text "",;
- BorderStyle 2,;
- OnMouseMove CLASS::MESSAGERECT_ONMOUSEMOVE,;
- Height 2.5303
-
- DEFINE TABBOX PAGETABBOX OF THIS;
- PROPERTY;
- Top 18,;
- DataSource "Array {'SQL Statement Builder', 'Templates', 'Examples'}",;
- Width 77.333,;
- ID 116,;
- OnSelChange CLASS::PAGETABBOX_ONSELCHANGE,;
- ColorHighLight "",;
- OnMouseMove CLASS::PAGETABBOX_ONMOUSEMOVE,;
- FontBold .F.,;
- Height 1
-
- DEFINE TEXT MESSAGETEXT OF THIS;
- PROPERTY;
- Left 1.3311,;
- Top 13.9473,;
- Width 74.1689,;
- Alignment 9,;
- ColorNormal "B/BTNFACE",;
- Text "",;
- FontBold .F.,;
- Height 2.2393
-
- DEFINE RECTANGLE MAINRECT OF THIS;
- PROPERTY;
- Left 0.5,;
- Top 0.1973,;
- Width 76.166,;
- PageNo 2,;
- Text "",;
- Height 15.9893
-
- DEFINE RADIOBUTTON TEMPLATE1RADIO OF THIS;
- PROPERTY;
- OnGotFocus CLASS::TEMPLATE1RADIO_ONGOTFOCUS,;
- Left 1.5,;
- Top 1,;
- Width 18.666,;
- ColorNormal "B/BtnFace",;
- PageNo 2,;
- Text "Show all data in ",;
- Group .T.,;
- Value .T.,;
- FontBold .F.,;
- Height 1
-
-
- DEFINE ENTRYFIELD TEMPLATE1ENTRY OF THIS;
- PROPERTY;
- Left 22.5,;
- Enabled .F.,;
- Top 1,;
- Width 48.5,;
- ColorNormal "N/BG+",;
- PageNo 2,;
- Value "",;
- FontBold .F.,;
- Height 1
-
- DEFINE TOOLBUTTON TEMPLATE1TOOLBUTTON OF THIS;
- PROPERTY;
- Left 72,;
- Top 1,;
- Width 4,;
- PageNo 2,;
- Group .T.,;
- OnClick CLASS::TEMPLATE1TOOLBUTTON_ONCLICK,;
- Height 1
-
- DEFINE RADIOBUTTON TEMPLATE2RADIO OF THIS;
- PROPERTY;
- OnGotFocus CLASS::TEMPLATE2RADIO_ONGOTFOCUS,;
- Left 1.5,;
- Top 3,;
- Width 14.166,;
- ColorNormal "B/BtnFace",;
- PageNo 2,;
- Text "Show fields",;
- Group .T.,;
- Value .T.,;
- FontBold .F.,;
- Height 0.875
-
- DEFINE LISTBOX TEMPLATE2LIST OF THIS;
- PROPERTY;
- Left 16.5,;
- Enabled .F.,;
- Top 3,;
- Width 17.5,;
- ColorNormal "N/BG+",;
- PageNo 2,;
- Multiple .T.,;
- ID 130,;
- ColorHighLight "WINDOWTEXT/WINDOW",;
- FontBold .F.,;
- Height 2.5
-
- DEFINE TEXT TEMPLATE2TEXT OF THIS;
- PROPERTY;
- Left 35,;
- Top 3,;
- Width 10.166,;
- ColorNormal "B/BtnFace",;
- PageNo 2,;
- Text "from table",;
- FontBold .F.,;
- Height 0.875
-
- DEFINE ENTRYFIELD TEMPLATE2ENTRY OF THIS;
- PROPERTY;
- Left 44,;
- Enabled .F.,;
- Top 3,;
- Width 27,;
- ColorNormal "N/BG+",;
- PageNo 2,;
- Value "",;
- FontBold .F.,;
- Height 1
-
- DEFINE TOOLBUTTON TEMPLATE2TOOLBUTTON OF THIS;
- PROPERTY;
- Left 72,;
- Top 3,;
- Width 4,;
- PageNo 2,;
- Group .T.,;
- OnClick CLASS::TEMPLATE2TOOLBUTTON_ONCLICK,;
- Height 1
-
- DEFINE RADIOBUTTON TEMPLATE3RADIO OF THIS;
- PROPERTY;
- OnGotFocus CLASS::TEMPLATE3RADIO_ONGOTFOCUS,;
- Left 1.5,;
- Top 6,;
- Width 44.166,;
- ColorNormal "B/BtnFace",;
- PageNo 2,;
- Text "Show customers whose orders totaled over ",;
- Group .T.,;
- Value .T.,;
- FontBold .F.,;
- Height 0.875
-
- DEFINE SPINBOX TEMPLATE3SPIN OF THIS;
- PROPERTY;
- Left 44.5,;
- Enabled .F.,;
- Top 6,;
- Width 16.166,;
- ColorNormal "N/BG+",;
- Picture "99999999999.99",;
- PageNo 2,;
- Function "$",;
- Value 500,;
- Step 10,;
- Rangemax 100,;
- Rangemin 1,;
- FontBold .F.,;
- Height 1
-
-
- DEFINE RECTANGLE ANSWERRECT OF THIS;
- PROPERTY;
- Left 0.5,;
- Top 10,;
- Width 76.5,;
- ColorNormal "n+/BtnFace",;
- PageNo 3,;
- Text "Answer: ",;
- FontBold .F.,;
- Height 6.1875
-
- DEFINE RECTANGLE COMMANDRECT OF THIS;
- PROPERTY;
- Left 0.5,;
- Top 0.1973,;
- Width 76.5,;
- ColorNormal "BtnFace",;
- PageNo 3,;
- Text "",;
- FontName "Arial",;
- FontSize 10,;
- FontBold .F.,;
- Height 6.9893
-
- DEFINE TABBOX SQLDBASETABBOX OF THIS;
- PROPERTY;
- Top 9,;
- DataSource "ARRAY {'SQL', 'DBASE'}",;
- Width 80,;
- ColorNormal "r/BtnFace",;
- PageNo 3,;
- Anchor 0,;
- ID 100,;
- OnSelChange CLASS::SQLDBASETABBOX_ONSELCHANGE,;
- ColorHighLight "",;
- FontBold .F.,;
- Height 1
-
- DEFINE COMBOBOX DESCRIPTCOMBO OF THIS;
- PROPERTY;
- Left 1.3311,;
- Top 1.5879,;
- Style 2,;
- DataSource "FIELD SQLDBASE->DESCRIPT",;
- Width 74.002,;
- DropDownHeight 8,;
- PageNo 3,;
- OnChange CLASS::DESCRIPTCOMBO_ONCHANGE,;
- FontBold .F.,;
- Height 1.2354
-
- DEFINE EDITOR COMMANDEDITOR OF THIS;
- PROPERTY;
- Left 1.6641,;
- DataLink "SQLDBASE->SQL_CMD",;
- Top 4,;
- Width 74.002,;
- ColorNormal "N/W*",;
- PageNo 3,;
- CUATab .T.,;
- Modify .F.,;
- FontSize 10,;
- FontBold .F.,;
- Height 2.875
-
- DEFINE TEXT COMMANDTEXT OF THIS;
- PROPERTY;
- Left 1,;
- Top 0.5,;
- Width 25,;
- ColorNormal "b/BtnFace",;
- PageNo 3,;
- Text "Command Description:",;
- FontBold .F.,;
- Height 1
-
- DEFINE TEXT COMPLETECOMMANDTEXT OF THIS;
- PROPERTY;
- Left 1,;
- Top 3,;
- Width 20,;
- ColorNormal "b/BtnFace",;
- PageNo 3,;
- Text "Complete Command:",;
- FontBold .F.,;
- Height 1
-
- DEFINE PUSHBUTTON EXECUTEBUTTON OF THIS;
- PROPERTY;
- Left 32.1641,;
- Top 7.2979,;
- Width 13.8359,;
- PageNo 3,;
- Text "Execute",;
- Group .T.,;
- OnClick CLASS::EXECUTEBUTTON_ONCLICK_SQL,;
- Default .T.,;
- FontBold .F.,;
- UpBitmap "RESOURCE #108",;
- Height 1.3887
-
- DEFINE BROWSE ANSWERBROWSE OF THIS;
- PROPERTY;
- Left 1.5,;
- Top 10.7979,;
- Width 74,;
- ColorNormal "B/BtnFace",;
- Visible .F.,;
- PageNo 3,;
- TabStop .F.,;
- CUATab .T.,;
- Modify .F.,;
- Height 5.0137
-
- DEFINE CLOSEBUTTON CLOSEBUTTON1 OF THIS;
- PROPERTY;
- Left 62.5,;
- Top 16.3975,;
- Width 14.166,;
- PageNo 3,;
- Group .T.,;
- FontBold .F.,;
- Height 1.5391
-
- Procedure FORM_OnOpen
- ****************************************************************************
-
- * Error trapping
- form.saveOnError = setto("on error")
- on error msgBox(message(), "Sorry", ALERT_MESSAGE + OK_BUTTON)
-
- set procedure to program(1) additive && Procedure file
- set procedure to &_dbwinhome.utils\Options.pop additive
- form.popupMenu = new OptionsPopup(form, "popup1")
- form.CleanupCurrentCommand = {;} && Function pointer to cleanup routine
- form.mouseOverControl = NULL && for each command, assigned in the
- && command's SetUp routine
- form.savePath = setto("path") && Initial path (reset on page 3)
- form.saveDirectory = setto("directory") && Initial directory
- CLASS::Page1Start()
- CLASS::Page2Start()
- CLASS::Page3Start()
-
-
- ****************************************************************************
-
- Procedure Page1Start
- ****************************************************************************
-
- *** Page 1 Setup
- * Options Popup
- * do Options.pop with form, "optionsPop"
-
- * Properties used throughout form
-
- form.statementAr = new array(MAX_STATEMENTS)
- form.clauseAr = new array(MAX_ALLOWED_CLAUSES)
- form.aggregateAr = new array(MAX_AGGREGATES)
- form.operatorAr = new array(MAX_OPERATORS)
- form.articleAr = new array(MAX_ARTICLES)
- form.sqlviewAr = new array(MAX_VIEWS)
-
- form.curCommand = "SELECT" && -- Current SQL command
- form.clauseCnt = 0 && -- Number of clauses in command
- form.insertValueCnt = 0 && -- Number of values to INSERT
- form.selectedFieldsAr = new array() && -- Array of selected fields
- form.selectedTablesAr = new array() && -- Array of selected tables
- form.serverName = ""
- form.tablesFromDatabase = .F.
-
- * Fill arrays
- form.statementAr[1] = "SELECT"
- form.statementAr[2] = "UPDATE"
- form.statementAr[3] = "INSERT"
- form.statementAr[4] = "DELETE"
-
- form.aggregateAr[1] = "SUM"
- form.aggregateAr[2] = "AVG"
- form.aggregateAr[3] = "MIN"
- form.aggregateAr[4] = "MAX"
- form.aggregateAr[5] = "COUNT"
-
- form.operatorAr[1] = "="
- form.operatorAr[2] = "<"
- form.operatorAr[3] = ">"
- form.operatorAr[4] = ">="
- form.operatorAr[5] = "<="
- form.operatorAr[6] = "<>"
- form.operatorAr[7] = "LIKE"
- form.operatorAr[8] = "IS NULL"
-
- form.articleAr[1] = " "
- form.articleAr[2] = " AND "
- form.articleAr[3] = " OR "
-
- * Set dataSource for combobox of commands
- form.statementCombo.datasource = "array form.statementAr"
- form.statementCombo.value = "SELECT"
-
- * Initialization for SELECT command
- CLASS::SetupSelect()
-
- ****************************************************************************
-
- Procedure Page2Start
- ****************************************************************************
-
- form.templateStatement = form.template1Radio.text
- form.template1Radio.datalink = "form.templateStatement"
- form.template2Radio.datalink = "form.templateStatement"
- form.template3Radio.datalink = "form.templateStatement"
- form.templateNum = 1
-
-
-
-
-
- ****************************************************************************
-
- Procedure Page3Start
- ****************************************************************************
-
- if select("Sqldbase") = 0
- form.view = "Sql.qbe"
- endif
- form.commandEditor.dataLink = "sqldbase->sql_cmd"
- form.answerBrowse.visible = .F.
- form.answerBrowse.alias = ""
- form.answerRect.text = "Answer: "
-
- form.sqlDbasePromptAr = new array(2)
- form.sqlDbasePromptAr[1] = "SQL"
- form.sqlDbasePromptAr[2] = "dBASE"
- form.sqlDbaseTabBox.datasource = "array form.sqlDbasePromptAr"
-
- * Environment that will be restored before every query is executed
- form.saveFields = setto("fields") && Original set of fields
- form.saveArea = str(workarea()) && Original work area
- form.lastQueryType = NO_QUERY && Indicates type of the last query
- form.dbaseCleanupCmdArray = new array() && Array for storing cleanup commands
- && after each dBASE query
- form.sqlCleanupCmdArray = new array() && Array for storing cleanup commands
- && after each SQL query
-
-
-
- ****************************************************************************
-
- Procedure FORM_OnClose
- ****************************************************************************
- private savePath, saveDirectory, saveOnError
-
- *** Page 1 (statement builder) cleanup
- if select("temp") > 0
- use in temp
- endif
- if select("temp2") > 0
- use in temp2
- endif
-
-
- *** Page 3 (examples) cleanup
- if form.pageNo = 3
- if form.dbaseCleanupCmdArray.size > 0 && if a query was performed
- form.RestoreEnvironment()
- endif
- endif
-
- close procedure &_dbwinhome.custom\Buttons.cc, program(1),;
- &_dbwinhome.utils\Options.pop
-
- savePath = form.savePath
- set path to &savePath
- saveDirectory = form.saveDirectory
- set directory to &saveDirectory
- saveOnError = form.saveOnError
- on error &saveOnError
-
-
-
- ****************************************************************************
-
- Procedure STATEMENTCOMBO_OnChange
- ****************************************************************************
-
- form.ClearSQLButton.OnClick() && Clear clauses from prev statement.
- form.CleanupCurrentCommand() && Function Pointer assigned in each
- && setup routine, referring to its
- && cleanup routine.
- form.curCommand = this.value && After cleanup, change command
- do case
- case this.value = "SELECT"
- form.SetUpSelect()
- case this.value = "UPDATE"
- form.SetUpUpdate()
- case this.value = "INSERT"
- form.SetUpInsert()
- case this.value = "DELETE"
- form.SetUpDelete()
- endcase
-
-
- ****************************************************************************
-
- Procedure PAGETABBOX_OnSelChange
- ****************************************************************************
- private notOnPage3, pageNo, savePath, saveDirectory
-
- if this.curSel <= form.PageCount()
- savePath = form.savePath
- saveDirectory = form.saveDirectory
- form.pageNo = this.curSel
- pageNo = form.pageNo
- notOnPage3 = (pageNo < 3)
- form.runSQLButton.visible = notOnPage3
- form.clearSQLButton.visible = notOnPage3
- form.showSQLButton.visible = notOnPage3
- form.okButton1.visible = notOnPage3
- form.cancelButton1.visible = notOnPage3
-
- do case
- case pageNo = 1
- CLASS::Page1Setup()
- case pageNo = 2
- CLASS::Page2Setup()
- case pageNo = 3
- CLASS::Page3Setup()
- endcase
- endif
-
-
- ****************************************************************************
-
- Procedure Page1Setup
-
- * Set up Statement Builder page when it is selected
- ****************************************************************************
- private savePath, saveDirectory
-
- savePath = form.savePath
- saveDirectory = form.saveDirectory
-
- set exact off
- set path to &savePath
- set directory to &saveDirectory
- form.okButton1.OnClick = CLASS::Page1OkButton1_OnClick
- form.runSQLButton.OnClick = CLASS::RunSQLButton_OnClick
- form.clearSQLButton.OnClick = CLASS::ClearSQLButton_OnClick
- form.OnMouseMove = CLASS::Form_OnMouseMove
-
-
-
- ****************************************************************************
-
- Procedure Page2Setup
-
-
- * Set up Templates page when it is selected
- ****************************************************************************
- private savePath, saveDirectory
-
- savePath = form.savePath
- saveDirectory = form.saveDirectory
-
- set exact on
- set path to &savePath
- set directory to &saveDirectory
- form.okButton1.OnClick = CLASS::Page2OkButton1_OnClick
- form.runSQLButton.OnClick = CLASS::TemplateRunSQLButton_OnClick
- form.clearSQLButton.OnClick = CLASS::TemplateClearSQLButton_OnClick
- form.clearSQLButton.OnClick()
- form.templateNum = 1
- form.template1Radio.SetFocus()
- form.OnMouseMove = .F.
-
-
- ****************************************************************************
-
- Procedure Page3Setup
-
- * Set up Examples page when it is selected
- ****************************************************************************
-
- set exact off
- form.view = "Sql.qbe"
- if file(_dbwinhome + "samples\animals.dbf")
- set path to &_dbwinhome.utils
- set directory to &_dbwinhome.samples
- form.executeButton.enabled = .T.
- else
- InformationMessage("The tables necessary for running the " +;
- "commands on this page do not exist in the " +;
- upper(_dbwinhome) + "SAMPLES directory. " +;
- chr(13) +;
- "You will be able to see " +;
- "the code, but not execute it.", ;
- "Info")
- form.executeButton.enabled = .F.
- endif
- CLASS::Page3Start()
- form.OnMouseMove = .F.
-
-
-
-
- ****************************************************************************
-
- Procedure ShowSQLButton_OnClick
- ****************************************************************************
-
- if form.pageNo = 1 && Statement Builder
- if CLASS::MakeSQLStatement(DISPLAY_WARNING)
- InformationMessage(form.sqlStatement, "SQL")
- endif
- else && PageNo = 2 (templates)
- if CLASS::MakeTemplateSQLStatement()
- InformationMessage(form.sqlStatement, "SQL")
- endif
- endif
-
-
-
-
- ****************************************************************************
-
- Procedure FieldsToolButton_OnClick
- ****************************************************************************
- private fieldsForm, i, selectedControl, selectedFieldCnt, selectedTableCnt,;
- serverName
-
- if empty(form.statementCombo.value)
- InformationMessage("You need to select a SQL command from the combobox" +;
- " at the top of the form.", "Info")
- else
- if .not. empty(form.fieldsEntry.value)
- form.clearSQLButton.OnClick() && Clear previous clauses
- endif
- create session
- set talk off
- set ldCheck off
-
- set procedure to &_dbwinhome.utils\GetField.wfm additive
- fieldsForm = new GetFieldForm()
- fieldsForm.mdi = .F.
- if form.curCommand = "INSERT" && If INSERT, only allow
- fieldsForm.oneTable = .T. && selection of one table
- endif
-
- selectedControl = fieldsForm.ReadModal()
-
- * If form wasn't CANCELed, Retrieve field and table information
- if type("selectedControl") = "O" .and. selectedControl.text <> "Cancel"
- form.tablesFromDatabase = fieldsForm.tablesFromDatabase
- if form.tablesFromDatabase && Set database if tables from
- serverName = fieldsForm.serverName && database
- form.serverName = serverName
- set database to &serverName
- endif
- * If all fields were selected, use ALL_FIELDS indicator
- if fieldsForm.allFields = .T. .and. (.not. form.curCommand = "INSERT")
- && All fields were selected
- form.fieldsEntry.value = ALL_FIELDS
- else
- CLASS::ListFieldsInEntryfield(fieldsForm)
- endif
-
- CLASS::ListTablesInEntryfield(fieldsForm)
-
- form.selectedTablesAr = fieldsForm.tableNamesAr
- if form.curCommand <> "INSERT" && If command wasn't INSERT
- form.selectedFieldsAr = CLASS::GetAllTableFields()
- else
- form.selectedFieldsAr = fieldsForm.selectedAr
- form.CreateInsertValues()
- endif
-
- show object form.fieldsEntry && Just in case
- show object form.tablesEntry
- endif
- close procedure &_dbwinhome.utils\GetField.wfm
- fieldsForm.Release()
- endif
-
-
-
- ****************************************************************************
-
- Procedure TablesToolButton_OnClick
- ****************************************************************************
- private tablesForm, selected, tableName, aliasName, saveDbType,;
- saveArea, tempTable, i, directory, fieldCnt, fullTableName
-
-
- saveDbType = setto("dbType")
-
- create session
- set talk off
- set ldCheck off
-
- set procedure to &_dbwinhome.utils\GetTable.wfm additive
-
- tablesForm = new GetTableForm()
- tablesForm.mdi = .F.
- selected = tablesForm.ReadModal()
- if type("selected") = "O" .and. type("tablesForm.selectedTable") = "C"
- if .not. empty(form.tablesEntry.value)
- form.clearSQLButton.OnClick() && Clear previous clauses
- endif
- tableName = tablesForm.selectedTable
-
- form.tablesFromDatabase = tablesForm.tablesFromDatabase
-
- CLASS::WriteTableInEntryfield(tablesForm) && Write table name
-
- form.selectedTablesAr = new array(1) && using correct
- if form.tablesFromDatabase && server/table syntax
- form.selectedTablesAr[1] = tableName
- fullTableName = tableName
- aliasName = tableName
- serverName = tablesForm.serverName
- form.serverName = serverName
- set database to &serverName
- else
- form.selectedTablesAr[1] = rtrim(form.tablesEntry.value)
- aliasName = GetAliasName(tableName)
- fullTableName = StripQuotes(form.selectedTablesAr[1])
- if right(tableName, 3) = ".db" && Paradox table
- set dbType to Paradox
- endif
- serverName = ""
- endif
-
-
- saveArea = str(workarea())
- select select()
- use (fullTableName)
- tempTable = "T" + left(funique(),7) && Temporary table for structure (some servers
- copy structure extended to (tempTable) && don't allow all numeric table names)
- use (tempTable)
- *** MEMO, BINARY, OLE fields aren't available in SQL statements
- count to fieldCnt for (.not. field_type $ MEMO_BINARY_OLE)
- go top
- form.selectedFieldsAr = new array(fieldCnt, 1)
- copy all to array form.selectedFieldsAr fields field_name;
- for (.not. field_type $ MEMO_BINARY_OLE)
-
- CLASS::CorrectFieldSyntax(aliasName, serverName) && Make sure fields
- use && use correct server syntax
- delete table (tempTable)
- select &saveArea
- set dbType to &saveDbType
-
- *** set up rest of controls for specific SQL commands
- if form.curCommand = "UPDATE"
- form.updateFieldsCombo.datasource = "array form.selectedFieldsAr"
- show object form.updateFieldsCombo
-
-
- *** Value doesn't show up right away
- form.updateFieldsCombo.value = form.selectedFieldsAr[1]
- form.updateFieldsCombo.OnChange()
- endif
- endif
- tablesForm.Release()
- close procedure &_dbwinhome.utils\GetTable.wfm
-
-
- ****************************************************************************
-
- Procedure AddClauseButton_OnClick
- ****************************************************************************
- private strCnt, strCntMinus1, articleCombo, valueCombo, fieldCombo
-
- if empty(form.tablesEntry.value)
- InformationMessage(;
- "You must select some fields from a table before adding a clause." +;
- chr(13) + "Click the tool button on the 'SELECT' line.",;
- "Info")
- else
- form.clauseCnt = form.clauseCnt + 1
- strCnt = TRIMSTR(form.clauseCnt)
-
- if form.clauseCnt = 1 && First clause for UPDATE
- DEFINE TEXT WHERETEXT OF FORM;
- PROPERTY;
- Text "WHERE",;
- Top this.top,;
- Height 1,;
- Left 2.5,;
- PageNo 1,;
- Width 8,;
- FontBold .F.,;
- ColorNormal "BTNTEXT/BTNFACE";
- CUSTOM;
- Value "WHERE"
-
- * Button is invisible so don't see it moving after this line
- this.top = this.top + .9
- this.text = "Add Clause"
- this.visible = .T.
-
- form.groupByButton.visible = .T.
- form.orderByButton.visible = .T.
-
- else
- strCntMinus1 = TRIMSTR(form.clauseCnt - 1)
- articleCombo = form.articleCombo&strCntMinus1
- if empty(articleCombo.value)
- articleCombo.value = " AND "
- endif
- endif
- DEFINE COMBOBOX FIELDCOMBO&strCnt OF FORM;
- PROPERTY;
- Top this.top,;
- FontBold .F.,;
- FontName "MS Serif",;
- FontSize 7,;
- Value "",;
- Style 2,;
- DropDownHeight 6,;
- Height 1,;
- Left 2.5,;
- PageNo 1,;
- Width 20,;
- ColorNormal "N/BTNFACE",;
- DataSource "array form.selectedFieldsAr",;
- OnChange CLASS::FIELDCOMBO_OnChange
-
- DEFINE COMBOBOX OPERATORCOMBO&strCnt OF FORM;
- PROPERTY;
- Top this.top,;
- FontBold .F.,;
- FontName "MS Serif",;
- FontSize 7,;
- Value "=",;
- Style 2,;
- DropDownHeight 6,;
- Height 1,;
- Left 23.0,;
- PageNo 1,;
- Width 9.335,;
- ColorNormal "WINDOWTEXT/WINDOW",;
- DataSource "array form.operatorAr",;
- OnChange CLASS::OPERATORCOMBO_OnChange
-
- DEFINE COMBOBOX VALUECOMBO&strCnt OF FORM;
- PROPERTY;
- Top this.top,;
- FontBold .F.,;
- FontName "MS Serif",;
- FontSize 7,;
- Value "",;
- Height 1,;
- Left 33,;
- PageNo 1,;
- Width 30.666,;
- Style 1,;
- ColorNormal "WINDOWTEXT/WINDOW",;
- OnChange CLASS::VALUECOMBO_OnChange,;
- OnLostFocus CLASS::VALUECOMBO_OnLostFocus
-
- if form.clauseCnt < MAX_ALLOWED_CLAUSES
- DEFINE COMBOBOX ARTICLECOMBO&strCnt OF FORM;
- PROPERTY;
- Top this.top,;
- FontBold .F.,;
- FontName "MS Serif",;
- FontSize 7,;
- Value " ",;
- Style 2,;
- DropDownHeight 6,;
- Height 1,;
- Left 65.5,;
- PageNo 1,;
- Width 10,;
- ColorNormal "WINDOWTEXT/WINDOW",;
- DataSource "array form.articleAr"
-
- this.top = this.top + 1.2
- else
- this.visible = .F.
- endif
- fieldCombo = form.fieldCombo&strCnt
- fieldCombo.OnChange()
- endif
-
-
- ****************************************************************************
-
- Procedure Page1OkButton1_OnClick
- ****************************************************************************
- local closeForm
-
- closeForm = .T.
- if CLASS::MakeSQLStatement(NO_WARNING)
- CLASS::SaveSQLStatement()
- closeForm = .T.
- else
- if ConfirmationMessage(;
- "The SQL statement is not complete." + chr(13) +;
- "Are you sure you want to close this form?", "Confirmation") == YES
- closeForm = .T.
- else
- closeForm = .F.
- endif
- endif
- if closeForm
- form.Close()
- endif
-
-
- ****************************************************************************
-
- Procedure SaveSQLStatement
- * Save created SQL statement to a file
- ****************************************************************************
- local fileName, fileHandle
-
- if ConfirmationMessage(;
- "Save created SQL statement to a .qbe file?", "Confirmation") == YES
- fileName = putFile(;
- "Enter a new file name for your SQL query", "*.qbe", "qbe")
- if .not. empty(fileName)
- fileHandle = fcreate(fileName)
- fputs(fileHandle, form.sqlStatement)
- fclose(fileHandle)
- endif
- endif
-
-
- ****************************************************************************
-
- Procedure RunSQLButton_OnClick
- ****************************************************************************
- private sqlStatement, saveArea, tableName
-
- if CLASS::MakeSQLStatement(DISPLAY_WARNING)
- sqlStatement = form.sqlStatement
- &sqlStatement
- if form.curCommand $ "UPDATE,INSERT,DELETE"
- if ConfirmationMessage("UPDATE, INSERT and DELETE statements do not " +;
- "produce an answer table. Would you like to " +;
- "see the resulting table?", "Confirm") = YES
-
- saveArea = str(workarea())
- select select()
- tableName = StripAllQuotes(form.tablesEntry.value)
- use (tableName)
- do Browse.wfm with .T. && Open browse form modally
- use
- select &saveArea
- endif
- else
- if reccount() > 0
- browse
- else
- InformationMessage("There are no records in this answer table.",;
- "Info")
-
- endif
- endif
- endif
-
-
- ****************************************************************************
-
- Procedure CLEARSQLBUTTON_OnClick
-
- * Clear all clause controls that have been defined
- ****************************************************************************
- private strCnt
-
- if form.curCommand = "INSERT"
- CLASS::InsertValuesClear()
- else
- CLASS::WhereClauseClear()
- CLASS::GroupByClauseClear()
- CLASS::OrderByClauseClear()
- endif
-
- CLASS::ClearTables()
-
- form.clauseCnt = 0
- form.insertValuesCnt = 0
- form.selectedTablesAr = new array(0)
- form.selectedFieldsAr = new array(0)
- form.serverName = ""
- form.tablesFromDatabase = .F.
-
-
- ****************************************************************************
-
- Procedure WhereClauseClear
-
- ****************************************************************************
- private strCnt, fieldCombo, operatorCombo, valueCombo, articleCombo,;
- clauseCnt
-
- clauseCnt = form.clauseCnt
- if clauseCnt > 0
- form.whereText.Release()
-
- form.addClauseButton.top = 3.1172
- form.addClauseButton.text = "Where"
- form.addClauseButton.visible = .T.
-
- for i = 1 to clauseCnt
- strCnt = TRIMSTR(i)
- fieldCombo = form.fieldCombo&strCnt
- operatorCombo = form.operatorCombo&strCnt
- valueCombo = form.valueCombo&strCnt
- fieldCombo.Release()
- operatorCombo.Release()
- valueCombo.Release()
- if i < MAX_ALLOWED_CLAUSES
- articleCombo = form.articleCombo&strCnt
- articleCombo.Release()
- endif
- next i
- form.clauseCnt = 0
- endif
-
- ****************************************************************************
-
- Procedure GroupByClauseClear
-
- ****************************************************************************
- private strCnt
-
- if type("form.groupByText") <> "U" && If Group By clause was selected
- form.groupByText.Release()
- form.groupByFieldCombo.Release()
- form.havingButton.Release()
- if type("form.havingText") <> "U" && If Having clause was selected
- form.havingText.Release()
- form.havingFieldsCombo.Release()
- form.havingOperatorCombo.Release()
- form.havingValueCombo.Release()
- endif
- endif
-
-
- ****************************************************************************
-
- Procedure OrderByClauseClear
-
- ****************************************************************************
- private strCnt
-
- if type("form.orderByText") <> "U" && If Order By clause was selected
- form.orderByText.Release()
- form.orderByFieldsCombo.Release()
- endif
-
-
- ****************************************************************************
-
- Procedure InsertValuesClear
-
- ****************************************************************************
- private strCnt, insertValueCnt,;
- insertValueText, insertValueCombo, insertComma, insertLastParen
-
- insertValueCnt = form.insertValueCnt
- if form.insertValueCnt > 0
- for i = 1 to insertValueCnt
- strCnt = TRIMSTR(i)
- insertValueText = form.insertValueText&strCnt
- insertValueCombo = form.insertValueCombo&strCnt
- insertValueText.Release()
- insertValueCombo.Release()
- if i < insertValueCnt
- insertComma = form.insertValueCommaText&strCnt
- insertComma.Release()
- else
- form.lastValueParenText.Top = form.statementText.top - .1
- form.lastValueParenText.Left = form.statementText.left +;
- len(form.statementText.value) + 1
- endif
- next i
- form.insertValueCnt = 0
- endif
-
-
- ****************************************************************************
-
- Procedure ClearTables
-
- ****************************************************************************
- private command
-
- command = form.curCommand
- do case
- case command = "SELECT"
- form.tablesEntry.value = ""
- form.fieldsEntry.value = ""
- case command = "UPDATE"
- form.tablesEntry.value = ""
- form.updateFieldsCombo.dataSource = ""
- form.updateFieldsCombo.value = ""
- form.updateValueCombo.dataSource = ""
- form.updateValueCombo.value = ""
- case command = "INSERT"
- form.tablesEntry.value = ""
- form.fieldsEntry.value = ""
- case command = "DELETE"
- form.tablesEntry.value = ""
- endcase
- close database
- set database to
-
-
- ****************************************************************************
-
- Function MakeSQLStatement(message)
- ****************************************************************************
- private madeStatement, control, sqlStatement, ending
-
- control = form.first
- sqlStatement = ""
-
- madeStatement = .F.
-
- * Make sure there is a table/fields to operate on
- if .not. empty(form.fieldsEntry.value) .or. .not. empty(form.tablesEntry.value)
- madeStatement = .T.
-
-
- * Make a string of all controls that are visible and have a value property
- do
- if control.pageNo = 1 .and. type("control.value") <> "U";
- .and. control.visible
- sqlStatement = sqlStatement + ALLTRIM(control.value) + " "
- endif
- control = control.before
- until control.hwnd = form.first.hwnd
-
- * Strip and/or if statement ended in an article
- ending = right(sqlStatement, 4)
- if (ending == "AND " .or. ending == " OR ")
- sqlStatement = left(sqlStatement, len(sqlStatement) - 4)
- endif
-
- ?sqlStatement
- form.sqlStatement = sqlStatement
- else
- madeStatement = .F.
- if message = DISPLAY_WARNING
- InformationMessage("The SQL statement is not complete. " +;
- "If you want to complete it, you should " +;
- "select a table/fields.", "Oops")
- endif
- endif
-
- return madeStatement
-
-
- ****************************************************************************
-
- Procedure SetUpSelect
- ****************************************************************************
-
- form.CleanupCurrentCommand = CLASS::CleanupSelect
- form.fieldsEntry.visible = .T.
- form.fieldsToolButton.visible = .T.
- form.tablesEntry.enabled = .F.
- form.tablesEntry.top = 1.8
- form.tablesToolButton.top = 1.8
- form.tablesToolButton.visible = .F.
- form.statementText.text = "FROM"
- form.statementText.value = "FROM"
-
- form.groupByButton.visible = .F.
- form.orderByButton.visible = .F.
-
- ****************************************************************************
-
- procedure CleanupSelect
- ****************************************************************************
-
- form.selectedFieldsAr = new array()
- form.selectedTablesAr = new array()
- form.fieldsEntry.value = ""
- form.tablesEntry.value = ""
-
-
- ****************************************************************************
-
- Procedure SetUpUpdate
- ****************************************************************************
-
- form.CleanupCurrentCommand = CLASS::CleanupUpdate
- form.fieldsEntry.visible = .F.
- form.fieldsToolButton.visible = .F.
- form.tablesEntry.top = .5 && Where Fields entry usually is
- form.tablesToolButton.visible = .T.
- form.tablesToolButton.top = .5
- form.statementText.text = "SET"
- form.statementText.value = "SET"
- form.tablesEntry.before = form.statementText
-
- form.groupByButton.visible = .F.
- form.orderByButton.visible = .F.
-
- DEFINE COMBOBOX UPDATEFIELDSCOMBO OF FORM;
- PROPERTY;
- ColorNormal "WINDOWTEXT/WINDOW",;
- DataSource "array form.selectedFieldsAr",;
- Style 2,;
- Top 1.8,;
- FontName "Ms Serif",;
- FontSize 7,;
- FontBold .F.,;
- Width 24.5,;
- Left 23,;
- Height 1,;
- PageNo 1,;
- OnChange CLASS::UPDATEFIELDSCOMBO_OnChange
-
- DEFINE TEXT UPDATEEQUALSTEXT OF FORM;
- PROPERTY;
- ColorNormal "BTNTEXT/BTNFACE",;
- Border .F.,;
- Text "=",;
- Top 1.8,;
- Left 49,;
- Height 1.26,;
- FontBold .F.,;
- PageNo 1;
- CUSTOM;
- Value "="
-
- DEFINE COMBOBOX UPDATEVALUECOMBO OF FORM;
- PROPERTY;
- ColorNormal "WINDOWTEXT/WINDOW",;
- Top 1.8,;
- Left 51.5,;
- Height 1.26,;
- Width 24,;
- FontBold .F.,;
- PageNo 1,;
- Value "",;
- OnChange CLASS::UPDATEVALUECOMBO_OnChange
-
-
- ****************************************************************************
-
- procedure CleanupUpdate
- ****************************************************************************
-
- form.fieldsEntry.visible = .T.
- form.fieldsToolButton.visible = .T.
- form.tablesEntry.top = 1.8
- form.tablesEntry.value = ""
- form.tablesToolButton.visible = .F.
- form.tablesToolButton.top = 1.8
- form.statementText.text = "FROM"
- form.statementText.value = "FROM"
- form.tablesEntry.before = form.tablesToolButton
-
- form.updateFieldsCombo.Release()
- form.updateEqualsText.Release()
- form.updateValueCombo.Release()
-
-
-
-
-
-
- ****************************************************************************
-
- Procedure SetUpInsert
- ****************************************************************************
-
- form.CleanupCurrentCommand = CLASS::CleanupInsert
- form.fieldsEntry.visible = .T.
- form.fieldsEntry.top = 1.8
- form.fieldsToolButton.visible = .T.
- form.fieldsToolButton.top = 1.8
- form.tablesEntry.top = .5
- form.tablesToolButton.visible = .F.
-
- form.addClauseButton.visible = .F.
- form.groupByButton.visible = .F.
- form.orderByButton.visible = .F.
-
- form.statementText.top = 3
- form.statementText.text = "VALUES ("
- form.statementText.value = "VALUES ("
- form.insertIntoText.visible = .T.
- form.insertIntoText.value = form.insertIntoText.text
- form.paren1Text.visible = .T.
- form.paren1Text.value = form.paren1Text.text
- form.paren2Text.visible = .T.
- form.paren2Text.value = form.paren2Text.text
-
- DEFINE TEXT LASTVALUEPARENTEXT OF FORM;
- PROPERTY;
- Alignment 3,; && Center left
- Top form.statementText.top - .05,;
- Left form.statementText.left + len(form.statementText.value) + 1,;
- Height 1,;
- Width 1,;
- FontBold .F.,;
- Text ")";
- CUSTOM;
- Value ")"
-
- * Make sure that when statement is put together, controls are in correct
- * order
-
- form.tablesEntry.before = form.paren1Text
- form.insertIntoText.before = form.tablesEntry
-
-
-
- ****************************************************************************
-
- Procedure CleanupInsert
- ****************************************************************************
-
- form.fieldsEntry.top = .5
- form.fieldsEntry.value = ""
- form.fieldsToolButton.top = .5
- form.tablesEntry.enabled = .F.
- form.tablesEntry.top = 1.8
- form.tablesToolButton.visible = .F.
- form.statementText.top = 1.8
- form.statementText.text = "FROM"
- form.statementText.value = "FROM"
-
- form.tablesEntry.before = form.tablesToolButton
- form.insertIntoText.before = form.tablesEntry
-
- form.insertIntoText.visible = .F.
- form.paren1Text.visible = .F.
- form.paren2Text.visible = .F.
-
- form.addClauseButton.visible = .T.
-
- form.lastValueParenText.Release()
-
- ****************************************************************************
-
- Procedure SetUpDelete
- ****************************************************************************
-
- form.CleanupCurrentCommand = CLASS::CleanupDelete
- form.fieldsEntry.visible = .F.
- form.fieldsToolButton.visible = .F.
- form.tablesEntry.top = 1.8
- form.tablesToolButton.visible = .T.
- form.tablesToolButton.top = 1.8
-
- form.groupByButton.visible = .F.
- form.orderByButton.visible = .F.
-
- form.statementText.text = "FROM"
- form.statementText.value = "FROM"
-
-
- ****************************************************************************
-
- Procedure CleanupDelete
- ****************************************************************************
-
- form.fieldsEntry.visible = .T.
- form.fieldsToolButton.visible = .T.
-
-
-
- ****************************************************************************
-
- Procedure GroupByButton_OnClick
- ****************************************************************************
-
-
- if empty(form.tablesEntry.value)
- InformationMessage(;
- "You must select some fields from a table before specifying a group." +;
- chr(13) + "Click the tool button on the 'SELECT' line.",;
- "Info")
- else
- form.addClauseButton.visible = .F. && Cannot add any more clauses
- DEFINE TEXT GROUPBYTEXT OF FORM;
- PROPERTY;
- Top this.top,;
- FontBold .F.,;
- PageNo 1,;
- Width 12,;
- ColorNormal "BTNTEXT/BTNFACE",;
- Text "GROUP BY",;
- Height 1.0293,;
- Left 2.5,;
- Border .F.;
- CUSTOM;
- Value "GROUP BY"
-
- DEFINE COMBOBOX GROUPBYFIELDCOMBO OF FORM;
- PROPERTY;
- Top this.top + .8,;
- FontName "MS Serif",;
- FontSize 7,;
- FontBold .F.,;
- PageNo 1,;
- Width 15.666,;
- ColorNormal "WINDOWTEXT/WINDOW",;
- Height 1,;
- Left 2.5,;
- Style 2,;
- DropDownHeight 6,;
- DataSource "array form.selectedFieldsAr",;
- Value form.selectedFieldsAr[1]
-
- if type("form.havingButton") = "U"
- DEFINE TOOLBUTTON HAVINGBUTTON OF FORM;
- PROPERTY;
- OnClick CLASS::HAVINGBUTTON_ONCLICK,;
- OnMouseMove CLASS::HAVINGBUTTON_ONMOUSEMOVE,;
- Top this.top + .8,;
- Width 13,;
- Text "Having",;
- Height 1,;
- Left 19,;
- Group .T.,;
- FontBold .F.
- endif
- this.visible = .F.
- endif
-
-
- ****************************************************************************
-
- Procedure HavingButton_OnClick
- ****************************************************************************
-
- * Don't need to check for table open because already checked in Group By
-
- this.visible = .F. && Button out of view while controls defined
- && over it
-
- DEFINE TEXT HAVINGTEXT OF FORM;
- PROPERTY;
- Top this.top,;
- FontBold .F.,;
- PageNo 1,;
- Width 8.166,;
- ColorNormal "BTNTEXT/BTNFACE",;
- Text "HAVING",;
- Height 1.0293,;
- Left 20,;
- Border .F.;
- CUSTOM;
- Value "HAVING"
-
- DEFINE COMBOBOX HAVINGFIELDSCOMBO OF FORM;
- PROPERTY;
- Top this.top,;
- FontName "Ms Serif",;
- FontSize 7,;
- FontBold .F.,;
- Value "",;
- Style 2,;
- DropDownHeight 6,;
- Height 1,;
- Left 29,;
- PageNo 1,;
- Width 20,;
- ColorNormal "WINDOWTEXT/WINDOW",;
- DataSource "array form.selectedFieldsAr",;
- OnChange CLASS::HAVINGFIELDSCOMBO_OnChange
-
- DEFINE COMBOBOX HAVINGOPERATORCOMBO OF FORM;
- PROPERTY;
- Top this.top,;
- FontName "Ms Serif",;
- FontSize 7,;
- FontBold .F.,;
- Value "=",;
- Style 2,;
- DropDownHeight 6,;
- Height 1,;
- Left 50.0,;
- PageNo 1,;
- Width 9.335,;
- ColorNormal "WINDOWTEXT/WINDOW",;
- DataSource "array form.operatorAr",;
- OnChange CLASS::OPERATORCOMBO_ONCHANGE && This routine used
- && for this control and
- && operatorCombo&cnt
-
- DEFINE COMBOBOX HAVINGVALUECOMBO OF FORM;
- PROPERTY;
- Top this.top,;
- FontName "Ms Serif",;
- FontSize 7,;
- FontBold .F.,;
- Value "",;
- Height 1,;
- Left 60,;
- PageNo 1,;
- Width 15,;
- Style 1,;
- ColorNormal "WINDOWTEXT/WINDOW",;
- DataSource "array form.selectedFieldsAr",;
- OnChange CLASS::HAVINGVALUECOMBO_OnChange
-
- form.havingFieldsCombo.OnChange()
- this.visible = .F.
-
- ****************************************************************************
-
- Procedure OrderByButton_OnClick
- ****************************************************************************
-
-
- if empty(form.tablesEntry.value)
- InformationMessage(;
- "You must select some fields from a table before specifying an order." +;
- chr(13) + "Click the tool button on the 'SELECT' line.",;
- "Info")
- else
- form.addClauseButton.visible = .F. && Cannot add any more clauses
- form.groupByButton.visible = .F.
- if type("form.havingButton") <> "U"
- form.havingButton.visible = .F.
- endif
- DEFINE TEXT ORDERBYTEXT OF FORM;
- PROPERTY;
- Top this.top,;
- PageNo 1,;
- Width 12,;
- ColorNormal "BTNTEXT/BTNFACE",;
- Text "ORDER BY",;
- Height 1,;
- Left 2.5,;
- Border .F.,;
- FontBold .F.;
- CUSTOM;
- Value "ORDER BY"
-
- DEFINE COMBOBOX ORDERBYFIELDSCOMBO OF FORM;
- PROPERTY;
- Top this.top,;
- FontName "Ms Serif",;
- FontSize 7,;
- FontBold .F.,;
- PageNo 1,;
- Width 15.666,;
- ColorNormal "WINDOWTEXT/WINDOW",;
- Height 1,;
- Left 19,;
- Style 2,;
- DropDownHeight 6,;
- DataSource "array form.selectedFieldsAr",;
- Value form.selectedfieldsAr[1]
-
- this.visible = .F.
- endif
-
-
- ****************************************************************************
-
- Procedure OperatorCombo_OnChange
- ****************************************************************************
-
- if this.value = "IS NULL"
- this.before.value = ""
- this.before.enabled = .F.
- else
- this.before.enabled = .T.
- endif
-
-
- *******************************************************************************
-
- Procedure STATEMENTCOMBO_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "statementCombo"
- form.mouseOverControl = "statementCombo"
- form.messageText.text = "Select a SQL command from this combobox"
- endif
-
-
- *******************************************************************************
-
- Procedure FIELDSENTRY_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "fieldsEntry"
- form.mouseOverControl = "fieldsEntry"
- form.messageText.text = "Type in the fields you would like to see " +;
- "in your SQL statement, or click on the tool " +;
- "button on the right to select the fields."
- endif
-
-
- *******************************************************************************
-
- Procedure TABLESENTRY_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "tablesEntry"
- form.mouseOverControl = "tablesEntry"
- form.messageText.text = "Type in the names of the tables you " +;
- "want processed in this SQL statement."
- endif
-
-
- *******************************************************************************
-
- Procedure FIELDSTOOLBUTTON_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "fieldsToolButton"
- form.mouseOverControl = "fieldsToolButton"
- form.messageText.text = "Press this button to select the fields " +;
- "you want processed in this SQL command."
- endif
-
- *******************************************************************************
-
- Procedure TABLESTOOLBUTTON_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "tablesToolButton"
- form.mouseOverControl = "tablesToolButton"
- form.messageText.text = "Press this button to select the tables " +;
- "you want processed in this SQL statement."
- endif
-
- *******************************************************************************
-
- Procedure ADDCLAUSEBUTTON_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "addClauseButton"
- form.mouseOverControl = "addClauseButton"
- form.messageText.text = "Press this button to create a condition " +;
- "that joins the selected tables, or to " +;
- "restrict the data that you will see in the " +;
- "answer table."
- endif
-
- *******************************************************************************
-
- Procedure GROUPBYBUTTON_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "groupByButton"
- form.mouseOverControl = "groupByButton"
- form.messageText.text = "Press this button to create a grouping " +;
- "of data in the answer table."
- endif
-
-
- *******************************************************************************
-
- Procedure HAVINGBUTTON_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "havingButton"
- form.mouseOverControl = "havingButton"
- form.messageText.text = "Press this button to make grouping " +;
- "of data in the answer table more specific."
- endif
-
-
- *******************************************************************************
-
- Procedure ORDERBYBUTTON_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "orderByButton"
- form.mouseOverControl = "orderByButton"
- form.messageText.text = "Press this button to select an order " +;
- "for viewing the selected data."
- endif
-
- *******************************************************************************
-
- Procedure STATEMENTRECT_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- form.mouseOverControl = "statementRect"
- form.messageText.text = ""
-
-
- *******************************************************************************
-
- Procedure MESSAGERECT_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- form.mouseOverControl = "messageRect"
- form.messageText.text = ""
-
-
- *******************************************************************************
-
- Procedure FORM_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- form.mouseOverControl = "form"
- form.messageText.text = ""
-
-
- *******************************************************************************
-
- Procedure PAGETABBOX_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "pageTabBox"
- form.mouseOverControl = "pageTabBox"
- form.messageText.text = "Select the 'Statement Builder' tab " +;
- "to create a SQL statement, the 'Templates' tab " +;
- "to use templates for creating a SQL statement, " +;
- "and the Examples tab for seeing examples of SQL and " +;
- "dBASE statements performing the same action."
- endif
-
-
- *******************************************************************************
-
- Procedure RUNSQLBUTTON_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "runSqlButton"
- form.mouseOverControl = "runSqlButton"
- form.messageText.text = "Run the created SQL statement. If an answer " +;
- "table is produced, display it in browse format."
- endif
-
-
- *******************************************************************************
-
- Procedure CLEARSQLBUTTON_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "clearSqlButton"
- form.mouseOverControl = "clearSqlButton"
- form.messageText.text = "Clear the displayed SQL statement. Display " +;
- "the statement as if nothing had been added " +;
- "to it yet."
- endif
-
-
-
- *******************************************************************************
-
- Procedure SHOWSQLBUTTON_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "showSqlButton"
- form.mouseOverControl = "showSqlButton"
- form.messageText.text = "Show the created SQL statement."
- endif
-
-
- *******************************************************************************
-
- Procedure OKBUTTON1_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "okButton1"
- form.mouseOverControl = "okButton1"
- form.messageText.text = "Close form with option to save statement " +;
- "to a .qbe file; also save statement " +;
- "as a property (sqlStatement) " +;
- "of the form, as well as output statement " +;
- "to the Command Results pane."
- endif
-
-
- *******************************************************************************
-
- Procedure CANCELBUTTON1_OnMouseMove(flags, col, row)
- *******************************************************************************
-
- if form.mouseOverControl <> "cancelButton1"
- form.mouseOverControl = "cancelButton1"
- form.messageText.text = "Close the form without saving any " +;
- "statement that might have been created."
- endif
-
-
-
- ****************************************************************************
-
- Procedure FIELDCOMBO_OnChange
- ****************************************************************************
- private strCnt, valueCombo
-
- strCnt = right(this.name, 1)
- valueCombo = form.valueCombo&strCnt
- CLASS::UpdateFields(this, valueCombo,;
- CLASS::ValueCombo_OnChange, "temp")
- this.SetFocus()
-
-
- ****************************************************************************
-
- Procedure VALUECOMBO_OnChange
- ****************************************************************************
- private strCnt, fieldCombo
-
- strCnt = right(this.name, 1)
- fieldCombo = form.fieldcombo&strCnt
- CLASS::UpdateValues(fieldCombo, this, "temp")
-
-
- ****************************************************************************
-
- Procedure VALUECOMBO_OnLostFocus
-
- * DOESN'T WORK NOW
- * Make sure that when leave combobox, if the operator is not "IS NULL", it is
- * set to "is null", so the statement would be valid
- ****************************************************************************
- private strCnt, operatorCombo
-
- if empty(this.value)
- strCnt = right(this.name, 1)
- operatorCombo = form.operatorCombo&strCnt
- if operatorCombo.value <> "IS NULL" && If operator expects
- REDEFINE COMBOBOX OPERATOR&strCnt OF FORM; && a value on the right
- PROPERTY;
- value "IS NULL"
- * operatorCombo.value = "IS NULL"
- * show object opertorCombo
- endif
- endif
-
-
-
-
-
- ****************************************************************************
-
- Procedure HAVINGFIELDSCOMBO_OnChange
- ****************************************************************************
-
- CLASS::UpdateFields(this, form.havingValueCombo,;
- CLASS::HavingValueCombo_OnChange, "temp2")
- this.SetFocus()
-
-
- ****************************************************************************
-
- Procedure HAVINGVALUECOMBO_OnChange
- ****************************************************************************
-
- CLASS::UpdateValues(form.havingFieldsCombo, this, "temp2")
-
-
- ****************************************************************************
-
- Procedure UPDATEFIELDSCOMBO_OnChange
- ****************************************************************************
-
- CLASS::UpdateFields(this, form.updateValueCombo,;
- CLASS::UpdateValueCombo_OnChange, "temp")
-
- this.SetFocus()
-
-
- ****************************************************************************
-
- Procedure UPDATEVALUECOMBO_OnChange
- ****************************************************************************
-
- CLASS::UpdateValues(form.updateFieldsCombo, this, "temp")
-
- ****************************************************************************
-
- Procedure UpdateFields
-
- ****************************************************************************
- * tempAlias is used in macro, so cannot be inline parameter
- param fieldCombo, valueCombo, ValueComboOnChange, tempAlias, serverName
-
- private fullTableName, fieldName, valueComboName, dataSource
-
- if select(tempAlias) > 0 && if already opened table in tempAlias
- use in (tempAlias)
- endif
- if form.tablesFromDatabase .and. empty(setto("database"))
- serverName = form.serverName
- open database &serverName
- set database to &serverName
- endif
-
- fullTableName = CLASS::GetFullTableName(;
- CLASS::GetTableName(fieldCombo.value))
- fieldName = CLASS::GetFieldName(fieldCombo.value)
-
- use (fullTableName) in select() alias &tempAlias
- if type(tempAlias + "->" + fieldName) $ MEMO_BINARY_OLE
- dataSource = ""
- else
- dataSource = "field " + tempAlias + "->" + fieldName
- endif
-
- valueComboName = valueCombo.name
-
- REDEFINE COMBOBOX &valueComboName OF FORM;
- PROPERTY;
- DataSource dataSource,;
- OnChange ValueComboOnchange
-
- valueCombo.width = min(valueCombo.width, 24) && Width to fit in form
-
- valueCombo.OnChange()
-
-
-
- ****************************************************************************
-
- Procedure UpdateValues(fieldCombo, valueCombo, tempAlias)
- ****************************************************************************
- private fieldName, fieldType, operatorCombo
-
- fieldName = tempAlias + "->" + CLASS::GetFieldName(fieldCombo.value)
- fieldType = type(fieldName)
- operatorCombo = fieldCombo.before && Object after fieldCombo
-
- do case
- case empty(valueCombo.value) && Empty value
- * (problem with onlostfocus)
- if operatorCombo.className = "COMBOBOX" && Comparison operator combobox
- operatorCombo.value = "IS NULL"
- else && Assignment operator
- valueCombo.value = "NULL"
- operatorCombo.value = "="
-
- endif
-
- case fieldType $ CHAR_LOGICAL_DATE && Char/logical/date value
- valueCombo.value = SurroundByQuotes(valueCombo.value)
- operatorCombo.value = "="
-
- case fieldType = NUMERIC && Numeric value
- valueCombo.value = CheckDecimalPoint(ltrim(valueCombo.value))
- operatorCombo.value = "="
-
- otherwise && Whatever
- valueCombo.value = "NULL"
- operatorCombo.value = "="
-
- endcase
-
-
-
-
-
- ****************************************************************************
-
- Procedure CreateInsertValues
-
- ****************************************************************************
- private top, left, strCnt, fullTableName, fieldName,;
- valueCombo, fullFieldName, valueCnt, serverName
-
- valueCnt = min(form.selectedFieldsAr.size, MAX_INSERT_VALUES)
- top = INSERT_START_TOP
- left = INSERT_START_LEFT
-
- * Open selected table in temporary area
- if select("temp") > 0 && if already opened table in "Temp"
- use in temp
- endif
- if form.tablesFromDatabase
- serverName = form.serverName
- if setto("database") <> serverName
- open database &serverName
- set database to &serverName
- endif
- fullTableName = form.tablesEntry.value && Server table syntax used
- else
- fullTableName = StripQuotes(form.tablesEntry.value)
- endif
- use (fullTableName) in select() alias temp
-
- for i = 1 to valueCnt
- fieldName = CLASS::GetFieldName(form.selectedFieldsAr[i])
- fullFieldName = "temp->" + fieldName
- strCnt = TRIMSTR(i)
-
- DEFINE TEXT INSERTVALUETEXT&strCnt OF FORM;
- PROPERTY;
- Top top - .7,;
- Left left,;
- Height .7,;
- Width INSERT_VALUE_WIDTH,;
- Alignment 7,; && Bottom center
- FontBold .F.,;
- FontItalic .T.,;
- ColorNormal "B/W",;
- Text lower(fieldName)
-
- DEFINE COMBOBOX INSERTVALUECOMBO&strCnt OF FORM;
- PROPERTY;
- Before form.lastValueParenText,; && So values appear in
- Top top,; && correct order when statement
- Left left,; && is created
- Height INSERT_VALUE_HEIGHT,;
- Width INSERT_VALUE_WIDTH,;
- FontBold .F.,;
- FontName "MS Serif",;
- FontSize 7,;
- Style 1,;
- DataSource "field temp->" + fieldName,;
- Value &fullFieldName,;
- OnChange CLASS::InsertUpdateValues
-
- if i < valueCnt
- DEFINE TEXT INSERTVALUECOMMATEXT&strCnt OF FORM;
- PROPERTY;
- Before form.lastValueParenText,; && This Before is also necessary
- Alignment 6,; && Bottom left && along with the one above for
- Top top,; && correct statement creation
- Left left + INSERT_VALUE_WIDTH + .1,;
- Height INSERT_VALUE_HEIGHT,;
- Width 1,;
- Text ",";
- CUSTOM;
- Value ","
- else
- form.lastValueParenText.top = top
- form.lastValueParenText.left = left + INSERT_VALUE_WIDTH + .5
- endif
-
- valueCombo = form.insertValueCombo&strCnt
- valueCombo.OnChange()
-
- top = INSERT_START_TOP +;
- ((INSERT_VALUE_HEIGHT + .7) * floor(i / INSERT_VALUES_PER_ROW))
- left = INSERT_START_LEFT +;
- mod((INSERT_VALUE_WIDTH + 1) * i,;
- (INSERT_VALUE_WIDTH + 1) * INSERT_VALUES_PER_ROW)
- next i
-
- form.insertValueCnt = valueCnt
-
-
-
-
-
- ****************************************************************************
-
- Procedure InsertUpdateValues
- ****************************************************************************
- private fieldName, fieldType, operatorObj
-
- fieldName = substr(this.dataSource, at("temp->", this.dataSource))
- fieldType = type(fieldName)
-
- do case
- case empty(this.value) && Empty value
- this.value = "NULL"
-
- case fieldType $ CHAR_LOGICAL_DATE && Char/logical/date value
- this.value = SurroundByQuotes(this.value)
-
- case fieldType = NUMERIC && Numeric value
- this.value = CheckDecimalPoint(ltrim(this.value))
-
- otherwise && Whatever
- this.value = "NULL"
- endcase
-
-
- *******************************************************************************
-
- function GetTableName(prompt)
-
- * Extract table name from prompt in form of 'tableName."fieldName"'
- *******************************************************************************
-
- return substr(prompt, 1, at(".", prompt) - 1)
-
-
- *******************************************************************************
-
- function GetFullTableName(name)
-
- * Get table name with directory from selectedTablesAr
- *******************************************************************************
- private tablesAr, i, lenName, found, aSize
-
- tablesAr = form.selectedTablesAr
- lenName = len(name)
- found = .F.
- i = 0
- aSize = tablesAr.size
- * Look for entry ending in name
- do while .not. found .and. i < aSize
- i = i + 1
- if right(tablesAr[i], lenName + 1) = name + '"'
- found = .T.
- endif
- enddo
-
- * Return value without surrounding quotations
- return StripQuotes(tablesAr[i])
-
-
- *******************************************************************************
-
- function GetFieldName(promptValue)
-
- * Extract field name w/out quotes from prompt in form of
- * 'tableName."fieldName"'
- *******************************************************************************
- local fieldStart, fieldName
-
- fieldStart = at('"', promptValue)
- if .not. empty(fieldStart)
- fieldName = StripQuotes(substr(promptValue, fieldStart))
- else && No quotes (some server tables)
- fieldStart = at(".", promptValue) + 1
- fieldName = substr(promptValue, fieldStart)
- endif
-
- return fieldName
-
-
-
- *******************************************************************************
-
- function GetTableAndField(fieldRef)
-
- * Extract field name w/out quotes from prompt in form of
- * 'tableName."fieldName"'
- *******************************************************************************
- local tableStart, tempFieldRef
-
- tempFieldRef = StripFieldQuotes(fieldRef)
- tableStart = at('.', tempFieldRef) + 1
- return substr(tempFieldRef, tableStart)
-
-
-
-
-
-
-
- *******************************************************************************
-
- function GetQuotedFieldName(promptValue)
-
- * Extract field name w/quotes from prompt in form of 'tableName."fieldName"'
- *******************************************************************************
- local fieldStart
-
- fieldStart = at('"', promptValue)
- return substr(promptValue, fieldStart)
-
-
-
-
-
-
- ****************************************************************************
-
- Function GetAllTableFields
-
- * Create array of all fields in tables from which fields were selected.
- ****************************************************************************
- private allFieldsAr, aliasName, fieldName, selectedTablesAr,;
- selectedTablesArSize, i, j, serverName, serverType,;
- separator, fieldFormula, aliasFormula
-
- if select("temp") > 0 && if already opened table in "Temp"
- use in temp
- endif
- selectedTablesAr = form.selectedTablesAr
- selectedTablesArSize = selectedTablesAr.size
- allFieldsAr = new array(0)
- separator = "."
- if form.tablesFromDatabase
- serverName = form.serverName
-
- open database &serverName
- set database to &serverName
- serverType = GetDatabaseType(serverName)
- do case
- case serverType = "ORACLE"
- fieldFormula = "SurroundByQuotes(fieldName)"
- aliasFormula = " "
- separator = ""
- case serverType = "INTRBASE"
- fieldFormula = "fieldName"
- aliasFormula = "dbf('temp')"
- case serverType = "SYBASE"
- fieldFormula = "substr(fieldName, at('.', fieldName) + 1)"
- aliasFormula = "substr(dbf('temp'), at('.', dbf('temp')) + 1)"
- otherwise
- fieldFormula = "fieldName"
- aliasFormula = " "
- endcase
- else
- fieldFormula = "SurroundByQuotes(fieldName)"
- aliasFormula = "StripExtension(StripDrive(dbf('temp')))"
- endif
- for i = 1 to selectedTablesArSize
- use (StripQuotes(selectedTablesAr[i])) in select() alias temp
- for j = 1 to fcount("temp")
- fieldName = field(j, "temp")
- if .not. type(fieldName) $ MEMO_BINARY_OLE && Make sure field is
- && not MEMO, OLE, or BINARY
- allFieldsAr.Add(&aliasFormula + separator + &fieldFormula)
- endif
- next i
- use in temp
- next i
-
- return allFieldsAr
-
-
- ****************************************************************************
-
- procedure ListFieldsInEntryfield(fieldsForm)
-
- * List fields in fields entryfield using syntax appropriate for current
- * database server or current table, if it is local.
- ****************************************************************************
- private serverName, serverType, selectedFieldCnt, i, fieldFormula, selectedAr
-
-
- selectedAr = fieldsForm.selectedAr
- selectedFieldCnt = selectedAr.size
-
- if .not. form.tablesFromDatabase && dBASE/Paradox table
- fieldFormula = "fieldsForm.selectedAr[i]"
- else && server table
- serverName = fieldsForm.serverName
- serverType = GetDatabaseType(serverName)
- do case
- case serverType = "ORACLE"
- fieldFormula = "form.GetQuotedFieldName(selectedAr[i])"
- case serverType = "INTRBASE"
- fieldFormula = "StripFieldQuotes(selectedAr[i])"
- case serverType = "SYBASE"
- fieldFormula = "form.GetTableAndField(selectedAr[i])"
- otherwise
- fieldFormula = "selectedAr[i]"
- endcase
- endif
-
- * Create list based on formula assigned above
- * Each field is modified according to formula, and then added to entryfield
-
- form.fieldsEntry.value = ""
- for i = 1 to selectedFieldCnt
- form.fieldsEntry.value = form.fieldsEntry.value +;
- &fieldFormula
- if i < selectedFieldCnt
- form.fieldsEntry.value = form.fieldsEntry.value + ", "
- endif
- next i
-
-
- ****************************************************************************
-
- procedure ListTablesInEntryfield(fieldsForm)
-
- * List tables in tables entryfield according to current database/table
- * syntax. This procedure is called after Fields were selected using the
- * Fields Tool Button
- ****************************************************************************
- private serverName, serverType, selectedTableCnt, i, tableFormula,;
- tableNamesAr
-
- tableNamesAr = fieldsForm.tableNamesAr
- selectedTableCnt = tableNamesAr.size
-
- if .not. form.tablesFromDatabase && dBASE/Paradox table
- tableFormula = "fieldsForm.tableNamesAr[i]"
- else
- serverName = fieldsForm.serverName
- serverType = GetDatabaseType(serverName)
- do case
- case serverType = "ORACLE"
- * Strip quotes around whole <alias.table> reference, and surround
- * only table name with quotes for Oracle
- tableFormula = "SurroundLastWordByQuotes(StripQuotes(tableNamesAr[i]))"
- case serverType = "INTRBASE"
- tableFormula = "StripQuotes(tableNamesAr[i])"
- case serverType = "SYBASE"
- tableFormula = "StripQuotes(tableNamesAr[i])"
- otherwise
- endcase
- endif
-
- * Create list based on formula assigned above
- * Each table is modified according to formula, and then added to entryfield
-
- form.tablesEntry.value = ""
- for i = 1 to selectedTableCnt
- form.tablesEntry.value = form.tablesEntry.value +;
- &tableFormula
- if i < selectedTableCnt
- form.tablesEntry.value = form.tablesEntry.value + ", "
- endif
- next i
-
-
-
-
- ****************************************************************************
-
- procedure WriteTableInEntryfield(tablesForm)
-
- * List one table in tables entryfield according to current database/table
- * syntax. This procedure is called after a single table was selected
- * using the tables tool button.
- ****************************************************************************
- private serverName, serverType, directory, tableFormula, tableName
-
- tableName = tablesForm.selectedTable
- if .not. form.tablesFromDatabase && dBASE/Paradox table
- directory = tablesForm.directoryText.text
- tableFormula = "SurroundByQuotes(tableName)"
- else
- serverName = tablesForm.serverName
- serverType = GetDatabaseType(serverName)
- do case
- case serverType = "ORACLE"
- * Strip quotes around whole <alias.table> reference, and surround
- * only table name with quotes for Oracle
- tableFormula = "SurroundLastWordByQuotes(StripQuotes(tableName))"
- case serverType = "INTRBASE"
- * No quotes at all for Interbase
- tableFormula = "StripQuotes(tableName)"
- case serverType = "SYBASE"
- tableFormula = "StripQuotes(tableName)"
- otherwise
- endcase
- endif
-
- * Write table name based on formula assigned above
-
- form.tablesEntry.value = ""
- form.tablesEntry.value = &tableFormula
-
-
-
-
- ****************************************************************************
-
- procedure CorrectFieldSyntax(aliasName, serverName)
-
- * Modifies field reference in form.selectedFieldsAr, using syntax dependent
- * on server.
- ****************************************************************************
- private serverType, selectedFieldCnt, i, fieldFormula, selectedAr
-
-
- selectedAr = form.selectedFieldsAr
- selectedFieldCnt = selectedAr.size
-
- if .not. form.tablesFromDatabase && dBASE/Paradox table
- fieldFormula = "aliasName + '.' + SurroundByQuotes(rtrim(selectedAr[i]))"
- else && server table
- serverType = GetDatabaseType(serverName)
- do case
- case serverType = "ORACLE"
- fieldFormula = "SurroundByQuotes(rtrim(selectedAr[i]))"
- case serverType = "INTRBASE"
- fieldFormula = "rtrim(selectedAr[i])"
- case serverType = "SYBASE"
- fieldFormula = "form.GetFieldName(rtrim(selectedAr[i]))"
- otherwise
- endcase
- endif
-
- * Modify form.selectedFieldsAr array based on formula assigned above
- for i = 1 to selectedFieldCnt
- selectedAr[i] = &fieldFormula
- next i
-
-
- ***************************** Page 2 Functions *****************************
-
-
-
-
- ****************************************************************************
-
- Procedure Page2OkButton1_OnClick
- ****************************************************************************
- private bCloseForm
-
- bCloseForm = .T.
- if CLASS::MakeTemplateSQLStatement()
- ?form.sqlStatement
- else
- if ConfirmationMessage("SQL statement not complete. Do you still " +;
- "want to close the form?",;
- "Confirm") = NO
- bCloseForm = .F.
- endif
- endif
-
- if bCloseForm
- form.Close()
- endif
-
-
- ****************************************************************************
-
- Function MakeTemplateSQLStatement
-
- * Make SQL statement from the selected template
- ****************************************************************************
- private templateNum, sqlStatement, i, field, fieldList, fieldCount,;
- samplesDir, bStatementComplete, aliasName, selectionMade
-
- bStatementComplete = .T.
- templateNum = form.templateNum
- sqlStatement = ""
-
- do case
- case templateNum = 1 && Show all fields of selected table
- if empty(form.template1Entry.value)
- bStatementComplete = .F.
- else
- sqlStatement = "SELECT * FROM " +;
- SurroundByQuotes(form.template1Entry.value)
- endif
- case templateNum = 2 && Show selected fields from a selected table
- if empty(form.template2Entry.value)
- bStatementComplete = .F.
- else
- sqlStatement = "SELECT "
- fieldList = form.template2List
- fieldCount = fieldList.Count()
- aliasName = GetAliasName(form.template2Entry.value)
- selectionMade = .F.
- for i = 1 to fieldCount
- field = fieldList.Selected(i)
- if .not. empty(field)
- selectionMade = .T.
- && Put all fields in quotations so no keyword conflict
- sqlStatement = sqlStatement + aliasName + "." +;
- SurroundByQuotes(field) + ", "
- endif
- next i
- if selectionMade
- && lose the last comma
- sqlStatement = left(sqlStatement, len(sqlStatement)- 2) + " "
- else
- sqlStatement = "SELECT * "
- endif
- sqlStatement = sqlStatement + "FROM " + ;
- SurroundByQuotes(form.template2Entry.value)
- endif
- case templateNum = 3 && Show all customers with orders over N
- * Check if Samples directory exists
- samplesDir = _dbwinhome + "SAMPLES"
- if .not. file(samplesDir + "\nul")
- InformationMessage(samplesDir + " directory does not exist.",;
- "Sorry")
- else
- sqlStatement = 'SELECT CUSTOMER."NAME", ORDERS."TOTAL" FROM ' +;
- '"' + samplesDir + '\CUSTOMER' + '", ' +;
- '"' + samplesDir + '\ORDERS' +'" WHERE ' +;
- 'CUSTOMER."CUSTOMER_N" = ORDERS."CUSTOMER_N" ' +;
- 'AND ORDERS."TOTAL" > ' +;
- CheckDecimalPoint(str(form.template3Spin.value, 10, 3))
- endif
- endcase
-
- if .not. bStatementComplete
- InformationMessage("Statement is not complete. A table must be opened.",;
- "Sorry")
- else
- form.sqlStatement = sqlStatement
- endif
-
- return bStatementComplete
-
-
- ****************************************************************************
-
- Procedure TemplateClearSQLButton_OnClick
-
- * Clears out SQL statement built by selected template
- ****************************************************************************
-
- CLASS::ClearTemplate()
- form.sqlStatement = ""
-
-
- ****************************************************************************
-
- Procedure ClearTemplate
-
- * Clears values entered for the currently selected template
- ****************************************************************************
- private templateNum
-
- templateNum = form.templateNum
-
- do case
- case templateNum = 1 && Show all fields of selected table
- form.template1Entry.value = ""
- case templateNum = 2 && Show selected fields from a selected table
- form.template2List.dataSource = ""
- form.template2Entry.value = ""
- case templateNum = 3 && Show all customers with orders over N
- form.template3Spin.value = 500.00
- endcase
-
-
-
- ****************************************************************************
-
- Procedure TemplateRunSQLButton_OnClick
-
- * Run SQL statement built by selected template
- ****************************************************************************
- private sqlStatement, saveArea, tableName
-
- if CLASS::MakeTemplateSQLStatement()
- sqlStatement = form.sqlStatement
- &sqlStatement
- if reccount() > 0
- browse
- else
- InformationMessage("There are no records in this answer table.",;
- "Info")
- endif
- endif
-
-
-
- ****************************************************************************
-
- Procedure DisableTemplateControls
-
- * Disable all controls tied to templates -- this is done before the specific
- * controls for the current template are enabled
- ****************************************************************************
-
- CLASS::ClearTemplate()
- form.template1ToolButton.enabled = .F.
- form.template2List.enabled = .F.
- form.template2ToolButton.enabled = .F.
- form.template3Spin.enabled = .F.
-
-
- ****************************************************************************
-
- Procedure TEMPLATE1RADIO_OnGotFocus
- ****************************************************************************
-
- CLASS::DisableTemplateControls()
- form.template2Radio.value = .F.
- form.template3Radio.value = .F.
- form.template1ToolButton.enabled = .T.
- form.templateNum = 1
-
-
- ****************************************************************************
-
- Procedure TEMPLATE2RADIO_OnGotFocus
- ****************************************************************************
-
- CLASS::DisableTemplateControls()
- form.template1Radio.value = .F.
- form.template3Radio.value = .F.
- form.template2list.enabled = .T.
- form.template2ToolButton.enabled = .T.
- form.templateNum = 2
-
-
- ****************************************************************************
-
- Procedure TEMPLATE3RADIO_OnGotFocus
- ****************************************************************************
-
- CLASS::DisableTemplateControls()
- form.template1Radio.value = .F.
- form.template2Radio.value = .F.
- form.template3spin.enabled = .T.
- form.templateNum = 3
-
-
- ****************************************************************************
-
- Procedure TEMPLATE1TOOLBUTTON_OnClick
- ****************************************************************************
- private fileName
-
- fileName = getFile("*.dbf", "Select a table")
- if .not. empty(fileName)
- form.template1Entry.value = fileName
- endif
-
-
- ****************************************************************************
-
- Procedure TEMPLATE2TOOLBUTTON_OnClick
- ****************************************************************************
- private fileName, saveArea, i, fCount
-
- fileName = getFile("*.dbf", "Select a table")
- if .not. empty(fileName)
- form.template2Entry.value = fileName
- saveArea = str(workarea()) && Save current workarea
- select select()
- use (fileName) && Open selected table
- fCount = fcount()
- form.template2List.structureAr = new array(fCount)
- for i = 1 to fcount && Save structure to array
- form.template2List.structureAr[i] = field(i)
- next i && Set list datasource to array
- form.template2List.dataSource = "array form.template2List.structureAr"
- use && Close table
- select &saveArea && Select original workarea
- endif
-
-
-
- ***************************** Page 3 Functions *****************************
-
-
-
- ****************************************************************************
-
- Procedure EXECUTEBUTTON_OnClick_SQL
- ****************************************************************************
- private mLines, sqlCmdArray
-
- form.RestoreEnvironment()
- mLines = memlines(sqldbase->sql_cmd)
- select select()
- sqlCmdArray = new array(mlines)
- store memo sqldbase->sql_cmd to array sqlCmdArray
- form.ExecuteCommandsFromArray(sqlCmdArray)
- form.answerRect.text = "Answer from SQL Query: "
- form.answerRect.colorNormal = "b+/BtnFace"
- form.answerBrowse.alias = alias() && Show query result in browse object
- form.answerBrowse.visible = .t. && Browse is invisible until a query is run
-
- * For cleanup
- form.sqlCleanupCmdArray = new array(memlines(sqldbase->sql_cln)) && Store cleanup
- store memo sqldbase->sql_cln to array form.sqlCleanupCmdArray && commands
-
- form.lastQueryType = SQL_QUERY
-
-
- ****************************************************************************
-
- Procedure EXECUTEBUTTON_OnClick_dBASE
- ****************************************************************************
- private mLines, dbCmdArray
-
- form.RestoreEnvironment() && Start with a clean environment
- mLines = memlines(sqldbase->dbase_cmd)
- select select()
- dbCmdArray = new array(mlines)
- store memo sqldbase->dbase_cmd to array dbCmdArray && Store dbase commands to array
- form.ExecuteCommandsFromArray(dbCmdArray)
- * don't want to lose fields in sqldbase table
- form.answerRect.text = "Answer from dBASE Query: "
- form.answerRect.colornormal = "b+/BtnFace"
- form.answerBrowse.visible = .f. && So don't see so much flashing
- form.answerBrowse.alias = alias() && Show query result in browse object
- form.answerBrowse.fields = setto("fields")
- if .not. empty(form.answerBrowse.fields)
- allFields = form.answerBrowse.fields + ;
- ",sqldbase->descript, sqldbase->sql_cmd, sqldbase->dbase_cmd, sqldbase->dbase_cln"
- set fields to &allFields
- endif
- form.answerBrowse.visible = .t.
-
- * For cleanup
- form.dbaseCleanupCmdArray = new array(memlines(sqldbase->dbase_cln)) && Store cleanup
- store memo sqldbase->dbase_cln to array form.dbaseCleanupCmdArray && commands
-
- form.lastQueryType = DBASE_QUERY
-
-
-
- ****************************************************************************
-
- Procedure SQLDBASETABBOX_OnSelChange
- ****************************************************************************
-
- do case
- case this.curSel = SQL_QUERY
- form.commandEditor.datalink = "sqldbase->sql_cmd"
- form.executeButton.OnClick = CLASS::EXECUTEBUTTON_ONCLICK_SQL
- case this.curSel = DBASE_QUERY
- form.commandEditor.datalink = "sqldbase->dbase_cmd"
- form.executeButton.OnClick = CLASS::EXECUTEBUTTON_ONCLICK_DBASE
- otherwise
- * No other tabs for now
- endcase
-
-
- ************************** Support Procedures ******************************
-
-
- ****************************************************************************
-
- Procedure DESCRIPTCOMBO_OnChange
- ****************************************************************************
- *** Changing combo values doesn't move record pointer
- private curAlias, thisValue
-
- curAlias = alias()
- select sqldbase
- thisValue = this.value
- locate for sqldbase->descript = thisValue
- go recno("sqldbase")
- select &curAlias
-
- form.answerBrowse.visible = .f.
- form.answerRect.text = "Answer: "
- form.answerRect.colorNormal = "n+/BtnFace"
-
- ****************************************************************************
-
- Procedure RestoreEnvironment
- ****************************************************************************
- private saveFields, saveArea, command, i, arSize
-
- do case
- case form.lastQueryType = SQL_QUERY
- * currently located in query area
- use
- form.ExecuteCommandsFromArray(form.sqlCleanupCmdArray)
- case form.lastQueryType = DBASE_QUERY
- form.ExecuteCommandsFromArray(form.dbaseCleanupCmdArray)
- otherwise
- * No query was performed. Nothing to cleanup
- endcase
-
- form.answerBrowse.fields = ""
- saveFields = form.saveFields
- saveArea = form.saveArea
- set fields to &saveFields
- select &saveArea
-
-
- ****************************************************************************
-
- procedure ExecuteCommandsFromArray(cmdArray)
- ****************************************************************************
- private command, i, cmdArraySize
-
- cmdArraySize = cmdArray.size
- for i = 1 to cmdArraySize
- command = cmdArray[i]
- do while right(command, 1) = ";"
- command = substr(command, 1, len(command) - 1)
- i = i + 1
- command = command + cmdArray[i]
- enddo
- &command
- next i
-
- ENDCLASS
-
-
- ********************************** Utility Functions **************************
-
- *******************************************************************************
-
- function StripExtension(fileName)
- *******************************************************************************
- private strippedName, periodLoc
-
- periodLoc = rat(".", fileName)
- if periodLoc > 0 && fileName has an extension
- strippedName = left(fileName, periodLoc - 1)
- else
- strippedName = fileName
- endif
-
- return strippedName
-
-
-
- *******************************************************************************
- function StripAllQuotes(value)
-
- * Strip quotes around whole value, or around last part of value
- *******************************************************************************
- private returnValue
-
- do case
- case left(value, 1) = '"' && Quotes around whole value
- returnValue = StripQuotes(value)
- case right(value, 1) = '"' && Quotes around last part of value
- returnValue = StripFieldQuotes(value)
- otherwise
- returnValue = value && No quotes -- return as is
- endcase
-
- return returnValue
-
-
-
-
-
- *******************************************************************************
- function StripQuotes(value)
-
- * Extract field name from prompt in form of 'tableName."fieldName"'
- *******************************************************************************
- local firstQuote, lastQuote, start, length, lenValue
-
- firstQuote = at('"', value)
- lastQuote = rat('"', value)
- lenValue = len(value)
- do case
- case (firstQuote = lastQuote) .and. firstQuote = 0
- start = 1
- length = lenValue
- case (firstQuote = lastQuote) .and. firstQuote = 1
- start = firstQuote + 1
- length = lenValue - 1
- case (firstQuote = lastQuote) .and. firstQuote = lenValue
- start = 1
- length = lastQuote - 1
- otherwise
- start = firstQuote + 1
- length = lastQuote - 2
- endcase
-
- return substr(value, start, length)
-
-
-
- ****************************************************************************
- Procedure SurroundByQuotes(value)
- ****************************************************************************
- private first, last, returnValue
-
- first = left(value, 1)
- last = right(value, 1)
-
- do case
- case first = '"' .and. last = '"' && Already surrounded by '
- returnValue = value
-
- case first = '"' .and. last = '"' && Already surounded by "
- returnValue = value
-
- case first $ ["'] && Only starts with a quote
- returnValue = value + first
-
- case last $ ["'] && Only ends with a quote
- returnValue = first + value
-
- otherwise && No quotes at all
- returnValue = '"' + value + '"'
- endcase
-
- return returnValue
-
-
- ****************************************************************************
- Procedure SurroundBySingleQuotes(value)
- ****************************************************************************
- private first, last, returnValue
-
- first = left(value, 1)
- last = right(value, 1)
-
- do case
- case first = "'" .and. last = "'" && Already surrounded by '
- returnValue = value
-
- case first = "'" .and. last = "'" && Already surounded by "
- returnValue = value
-
- case first $ ["'] && Only starts with a quote
- returnValue = value + first
-
- case last $ ["'] && Only ends with a quote
- returnValue = first + value
-
- otherwise && No quotes at all
- returnValue = "'" + value + "'"
- endcase
-
- return returnValue
-
-
-
- ****************************************************************************
- Procedure SurroundLastWordByQuotes(value)
- ****************************************************************************
- private afterPeriod
-
- afterPeriod = rat(".", value) + 1
- return stuff(value, afterPeriod, 0, '"') + '"'
-
-
-
- ****************************************************************************
- Procedure StripFieldQuotes(fieldRef)
- ****************************************************************************
- private firstQuoteLoc, fieldRefLen, tempNewRef
-
- firstQuoteLoc = at('"', fieldRef)
- fieldRefLen = len(fieldRef)
- tempNewRef = left(fieldRef, fieldRefLen - 1)
- return stuff(tempNewRef, firstQuoteLoc, 1, "")
-
-
- ****************************************************************************
-
- Procedure StripDrive(directory)
- ****************************************************************************
-
- return substr(directory, at(":", directory) + 1)
-
-
-
- ****************************************************************************
-
- Procedure GetAliasName(fullTableName)
- ****************************************************************************
- private tableName
-
- tableName = substr(fullTableName, rat("\", fullTableName) + 1)
-
- return StripExtension(tableName)
-
-
-
- ****************************************************************************
- Function CheckDecimalPoint(numberStr)
- ****************************************************************************
- private decPoint, decPointLoc, returnStr
-
- decPoint = setto("point")
- decPointLoc = at(decPoint, numberStr)
-
- * Replace decimalPoint with "." if it exists and is different
- if decPointLoc > 0 .and. decPoint <> "."
- returnStr = stuff(numberStr, decPointLoc, 1, ".")
- else
- returnStr = numberStr
- endif
-
- return returnStr
-
-
-
-
-
- ****************************************************************************
- function GetDatabaseType(databaseName)
-
- * Retrieve database type by using dbiGetDatabaseDesc IDAPI function.
- ****************************************************************************
- private iResult, databaseInfo
-
- * Function for retrieving database information
- extern CINT dbiGetDatabaseDesc(CSTRING, CPTR) Idapi01.dll
-
- databaseInfo = space(DATABASE_INFO_LEN) && Buffer for database info
- iResult = dbiGetDatabaseDesc(databaseName, databaseInfo)
-
- return rtrim(substr(databaseInfo, DATABASE_TYPE_LOC))
-
-
-
-
- ****************************************************************************
- function ServerTableSyntax(tableName, databaseName)
-
- * Retrieve database type by using dbiGetDatabaseDesc IDAPI function.
- ****************************************************************************
- private iResult, databaseInfo
-
- * Function for retrieving database information
- extern CINT dbiGetDatabaseDesc(CSTRING, CPTR) Idapi01.dll
-
- databaseInfo = space(DATABASE_INFO_LEN) && Buffer for database info
- iResult = dbiGetDatabaseDesc(databaseName, databaseInfo)
-
- return rtrim(substr(databaseInfo, DATABASE_TYPE_LOC))
-
-
- ****************************************************************************
-
- Procedure HandleDatabaseError
- ****************************************************************************
- do case
- case error() = ERROR_NO_SUCH_DATABASE
- InformationMessage("Sorry, Database doesn't exist.", "Oops")
-
- otherwise
- InformationMessage("Sorry, couldn't connect to database", "Oops")
-
- endcase
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-